package cn.wolfcode.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.wolfcode.business.domain.Appointment;
import cn.wolfcode.business.domain.Statement;
import cn.wolfcode.business.mapper.AppointmentMapper;
import cn.wolfcode.business.mapper.StatementMapper;
import cn.wolfcode.business.vo.StatementItemVO;
import cn.wolfcode.common.exception.ServiceException;
import cn.wolfcode.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.StatementItemMapper;
import cn.wolfcode.business.domain.StatementItem;
import cn.wolfcode.business.service.IStatementItemService;

/**
 * 结算单明细Service业务层处理
 * 
 * @author dafei
 * @date 2024-03-04
 */
@Service
public class StatementItemServiceImpl implements IStatementItemService 
{
    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private StatementMapper statementMapper;

    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 查询结算单明细
     * 
     * @param id 结算单明细主键
     * @return 结算单明细
     */
    @Override
    public StatementItem selectStatementItemById(Long id)
    {
        return statementItemMapper.selectStatementItemById(id);
    }

    /**
     * 查询结算单明细列表
     * 
     * @param statementItem 结算单明细
     * @return 结算单明细
     */
    @Override
    public List<StatementItem> selectStatementItemList(StatementItem statementItem)
    {
        return statementItemMapper.selectStatementItemList(statementItem);
    }

    /**
     * 新增结算单明细
     * 
     * @param statementItem 结算单明细
     * @return 结果
     */
    @Override
    public int insertStatementItem(StatementItem statementItem)
    {
        return statementItemMapper.insertStatementItem(statementItem);
    }

    /**
     * 修改结算单明细
     * 
     * @param statementItem 结算单明细
     * @return 结果
     */
    @Override
    public int updateStatementItem(StatementItem statementItem)
    {
        return statementItemMapper.updateStatementItem(statementItem);
    }

    /**
     * 批量删除结算单明细
     * 
     * @param ids 需要删除的结算单明细主键
     * @return 结果
     */
    @Override
    public int deleteStatementItemByIds(Long[] ids)
    {
        return statementItemMapper.deleteStatementItemByIds(ids);
    }

    /**
     * 删除结算单明细信息
     * 
     * @param id 结算单明细主键
     * @return 结果
     */
    @Override
    public int deleteStatementItemById(Long id)
    {
        return statementItemMapper.deleteStatementItemById(id);
    }

    @Override
    public void saveItem(StatementItemVO vo) {
        //条件
        Statement statement = statementMapper.selectStatementById(vo.getStatementId());
        if(statement == null){
            throw new ServiceException("参数异常");
        }

        if(statement.getStatus() != Statement.STATUS_CONSUME){
            throw new ServiceException("只有结算单在消费中状态才允许操作");
        }

        //数据改动
        //item明细入库
        List<StatementItem> statementItems = vo.getStatementItems();

        BigDecimal totalAmount = BigDecimal.ZERO;   //总金额
        BigDecimal totalQuantity = BigDecimal.ZERO;  //总数量

        statementItemMapper.deleteByStatementId(vo.getStatementId());

        if(statementItems != null && statementItems.size() > 0){
            for (StatementItem statementItem : statementItems) {
                totalQuantity = totalQuantity.add(new BigDecimal(statementItem.getItemQuantity()));

                //总金额 +=  单价 * 数量
                totalAmount = totalAmount.add(statementItem.getItemPrice().multiply(new BigDecimal(statementItem.getItemQuantity())));
                //item明细入库
                statementItemMapper.insertStatementItem(statementItem);
            }
        }

        //结算单数据更新---totalAmount   totalQuantity   discountAmount
        if(totalAmount.compareTo(vo.getDiscountPrice())  < 0){
            throw new ServiceException("总金额要大于或者等于优惠价格");
        }

        statementMapper.updateStatementAmount(vo.getStatementId(), totalAmount, totalQuantity, vo.getDiscountPrice());
    }

    @Override
    public void pay(Long id) {
        //条件
        Statement statement = statementMapper.selectStatementById(id);
        if(statement == null){
            throw new ServiceException("参数异常");
        }

        if(statement.getStatus() != Statement.STATUS_CONSUME){
            throw new ServiceException("只有结算单在消费中状态才允许操作");
        }



        statement.setStatus(Statement.STATUS_PAID);
        statement.setPayTime(new Date()); //收款时间
        statement.setPayeeId(SecurityUtils.getUserId());  //收款人

        statementMapper.updateStatement(statement);


        //当前如果是绑定了预约单的结算单 需要修改预约单状态--已支付
        Long appointmentId = statement.getAppointmentId();
        if(appointmentId != null){
            Appointment appointment = appointmentMapper.selectAppointmentById(appointmentId);
            appointment.setStatus(Appointment.STATUS_PAYED);
            appointmentMapper.updateAppointment(appointment);
        }
    }
}
